package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.base.BaseModel;
import com.sphd.beautysalon.info.PushScaleInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.SetPushPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
* Created by Hu on 2016/12/13
*/

public class SetPushModelImpl implements SetPushModel, VolleyJsonObjectCallBack {
    SetPushPresenterImpl setPushPresenter;
    @Override
    public void sendUrl(String url, SetPushPresenterImpl setPushPresenter) {
        this.setPushPresenter=setPushPresenter;
        BaseModel.volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void getSetVal(String url, SetPushPresenterImpl setPushPresenter) {
        this.setPushPresenter=setPushPresenter;
        BaseModel.volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    List<PushScaleInfo> pushScaleInfos = JsonUtil.getListFromString(response.getString("list"),PushScaleInfo.class);
                    setPushPresenter.recList(pushScaleInfos);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            setPushPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            int res=response.getInt("res");
            setPushPresenter.recRes(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        setPushPresenter.sendError(error);
    }
}